<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>

<style type="text/css">
	
@media print {
.stats {
        background-color: white;
        height: 100%;
        width: 100%;
        position: fixed;
        top: 0;
        left: 0;
        margin: 0;
        padding: 15px;
        font-size: 14px;
        line-height: 18px;
    }
}

</style>

<div id="viewtrainingspresencecumucontent">

<h4>View Training Presence by Season - Cumulative</h4>
<form>
	
<select class="form-control"  name="season" id="select1">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM trainingpresence";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

</form>

<?php

if (isset($_POST['season'])) {
	
	$setseason = $_POST['season'];

	$sql1 = "SELECT playerID, COUNT(presence) 
	AS totalpresence, count(case presence when 'yes' then 1 else null end) AS totalpresent
	FROM trainingpresence
	WHERE season = '$setseason'
	GROUP BY playerID";

	$sql2 = "SELECT trainingpresence.playerID, COUNT(trainingpresence.presence) 
	AS totalpresence, count(case trainingpresence.presence when 'yes' then 1 else null end) AS totalpresent, users.firstname, users.lastname
	FROM trainingpresence
	JOIN users 
	ON users.user_key = trainingpresence.playerID
	WHERE trainingpresence.season = '$setseason'
	GROUP BY playerID
	ORDER BY users.playernumber";

	$result2 = $db->query($sql2);

?>


<br>
<div class="table-responsive stats">
<h4>Cumulative Trainingpresence of <?= $setseason; ?></h4>

<p>
<button onclick="myFunction()" class="btn btn-primary btn-sm hidden-print">Print Presence Stats</button>
</p>

<table class="table table-hover table-striped">
    <thead>
      <tr>
        <th colspan="2">Name</th>
        <th>Presence</th>
      </tr>
    </thead>
    <tbody>
<?php
	while ($row2 = $result2->fetch_assoc()) {

		$firstname = $row2['firstname'];
		$lastname = $row2['lastname'];		
		$totalpresence = $row2['totalpresence'];
		$totalpresent = $row2['totalpresent'];

		$presenceperc = round($totalpresent/$totalpresence*100);

		echo "
		<tr>
		<td>$firstname</td>
		<td>$lastname</td>
		<td>$totalpresent/$totalpresence ($presenceperc%)</td>
		</tr>
		";

}

?>



    </tbody>
 </table>




</div>

<?php } ?>
</div>


<script type="text/javascript">
	
	$("#select1").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewpresencecumu.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtrainingspresencecumucontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingspresencecumucontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

function myFunction() {
    window.print();
}

</script>